
struct pt_regs {
	union {
		struct user_pt_regs user_regs;
		struct {
			u64 regs[31];
			u64 sp;
			u64 pc;
			u64 pstate;
		};
	};
	u64 orig_x0;
#ifdef __AARCH64EB__
	u32 unused2;
	s32 syscallno;
#else
	s32 syscallno;
	u32 unused2;
#endif

	u64 orig_addr_limit;
	/* Only valid when ARM64_HAS_IRQ_PRIO_MASKING is enabled. */
	u64 pmr_save;
	u64 stackframe[2];
};
DEFINE(S_X0,                  offsetof(struct pt_regs, regs[0]));
DEFINE(S_X2,                  offsetof(struct pt_regs, regs[2]));
DEFINE(S_X4,                  offsetof(struct pt_regs, regs[4]));
DEFINE(S_X6,                  offsetof(struct pt_regs, regs[6]));
DEFINE(S_X8,                  offsetof(struct pt_regs, regs[8]));
DEFINE(S_X10,                 offsetof(struct pt_regs, regs[10]));
DEFINE(S_X12,                 offsetof(struct pt_regs, regs[12]));
DEFINE(S_X14,                 offsetof(struct pt_regs, regs[14]));
DEFINE(S_X16,                 offsetof(struct pt_regs, regs[16]));
DEFINE(S_X18,                 offsetof(struct pt_regs, regs[18]));
DEFINE(S_X20,                 offsetof(struct pt_regs, regs[20]));
DEFINE(S_X22,                 offsetof(struct pt_regs, regs[22]));
DEFINE(S_X24,                 offsetof(struct pt_regs, regs[24]));
DEFINE(S_X26,                 offsetof(struct pt_regs, regs[26]));
DEFINE(S_X28,                 offsetof(struct pt_regs, regs[28]));
DEFINE(S_FP,                  offsetof(struct pt_regs, regs[29]));
DEFINE(S_LR,                  offsetof(struct pt_regs, regs[30]));
DEFINE(S_SP,                  offsetof(struct pt_regs, sp));
DEFINE(S_PSTATE,              offsetof(struct pt_regs, pstate));
DEFINE(S_PC,                  offsetof(struct pt_regs, pc));

DEFINE(S_FRAME_SIZE,		  sizeof(struct pt_regs));
DEFINE(S_STACKFRAME,          offsetof(struct pt_regs, stackframe));


#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
/*
 * Due to -fpatchable-function-entry=2, the compiler has placed two NOPs before
 * the regular function prologue. For an enabled callsite, ftrace_init_nop() and
 * ftrace_make_call() have patched those NOPs to:
 *
 * 	MOV	X9, LR
 * 	BL	<entry>
 *
 * ... where <entry> is either ftrace_caller or ftrace_regs_caller.
 *
 * Each instrumented function follows the AAPCS, so here x0-x8 and x19-x30 are
 * live, and x9-x18 are safe to clobber.
 *
 * We save the callsite's context into a pt_regs before invoking any ftrace
 * callbacks. So that we can get a sensible backtrace, we create a stack record
 * for the callsite and the ftrace entry assembly. This is not sufficient for
 * reliable stacktrace: until we create the callsite stack record, its caller
 * is missing from the LR and existing chain of frame records.
 */
	.macro  ftrace_regs_entry, allregs=0
	/* Make room for pt_regs, plus a callee frame */
	sub	sp, sp, #(S_FRAME_SIZE + 16)

	/* Save function arguments (and x9 for simplicity) */
	/* 保存x0~x9到栈中 */
	stp	x0, x1, [sp, #S_X0]
	stp	x2, x3, [sp, #S_X2]
	stp	x4, x5, [sp, #S_X4]
	stp	x6, x7, [sp, #S_X6]
	stp	x8, x9, [sp, #S_X8]

	/* Optionally save the callee-saved registers, always save the FP */
	.if \allregs == 1
	/* 保存所有寄存器到栈中 */
	stp	x10, x11, [sp, #S_X10]
	stp	x12, x13, [sp, #S_X12]
	stp	x14, x15, [sp, #S_X14]
	stp	x16, x17, [sp, #S_X16]
	stp	x18, x19, [sp, #S_X18]
	stp	x20, x21, [sp, #S_X20]
	stp	x22, x23, [sp, #S_X22]
	stp	x24, x25, [sp, #S_X24]
	stp	x26, x27, [sp, #S_X26]
	stp	x28, x29, [sp, #S_X28]
	.else
	str	x29, [sp, #S_FP]
	.endif

	/* Save the callsite's SP and LR */
	/* 将sp +  S_FRAME_SIZE + 16 赋值给x10 */
	add	x10, sp, #(S_FRAME_SIZE + 16)
	/* 将x9(LR) x10(sp) 保存到栈对应的S_LR和S_SP处 */
	stp	x9, x10, [sp, #S_LR]

	/* Save the PC after the ftrace callsite，保存调用着的pc */
	str	x30, [sp, #S_PC]

	/* Create a frame record for the callsite above pt_regs */
	/* x29和x9保存到 16字节处 ，保存栈帧*/
	stp	x29, x9, [sp, #S_FRAME_SIZE]
	/* x29 = sp +  S_FRAME_SIZE */
	add	x29, sp, #S_FRAME_SIZE /* 栈帧指向sp + #S_FRAME_SIZE */

	/* 创建我们的栈帧，就在16个字节中 */
	/* Create our frame record within pt_regs. */
	stp	x29, x30, [sp, #S_STACKFRAME]
	add	x29, sp, #S_STACKFRAME
	.endm

ENTRY(ftrace_regs_caller)
	ftrace_regs_entry	1
	b	ftrace_common
ENDPROC(ftrace_regs_caller)


ENTRY(ftrace_common)
	sub	x0, x30, #AARCH64_INSN_SIZE	// ip (callsite's BL insn)，被ftrace函数的bl 处的pc
	mov	x1, x9				// parent_ip (callsite's LR)，调用被ftrace函数的pc
	ldr_l	x2, function_trace_op		// op  function_trace_op赋值给x2, sp 赋值给x3
	mov	x3, sp				// regs

GLOBAL(ftrace_call)
	bl	ftrace_stub  /* 已经被替换为 ftrace_ops_list_func */

#ifdef CONFIG_FUNCTION_GRAPH_TRACER
GLOBAL(ftrace_graph_call)		// ftrace_graph_caller();
	nop				// If enabled, this will be replaced
					// "b ftrace_graph_caller"
#endif

/*
 * At the callsite x0-x8 and x19-x30 were live. Any C code will have preserved
 * x19-x29 per the AAPCS, and we created frame records upon entry, so we need
 * to restore x0-x8, x29, and x30.
 */
ftrace_common_return:
	/* Restore function arguments */
	ldp	x0, x1, [sp]
	ldp	x2, x3, [sp, #S_X2]
	ldp	x4, x5, [sp, #S_X4]
	ldp	x6, x7, [sp, #S_X6]
	ldr	x8, [sp, #S_X8]

	/* Restore the callsite's FP, LR, PC */
	ldr	x29, [sp, #S_FP]
	ldr	x30, [sp, #S_LR]
	ldr	x9, [sp, #S_PC]

	/* Restore the callsite's SP */
	add	sp, sp, #S_FRAME_SIZE + 16

	/* 返回到被ftrace函数，bl        ftrace_regs_caller 之后的的地址
	 * 如何在ops的函数中修改pc，则跳转到新的函数地方，因为保存了x0~x8
	 * 寄存器，所以hook的函数可以获取函数参数，如果不修改pc着继续执行
	 * 原函数
	 */
	ret	x9
ENDPROC(ftrace_common)

